
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.Collections.Generic;

namespace Tathata.Web.Administration
{
    public partial class Administration_CustomerDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Customer customer = CustomerManager.GetByCustomerID(this.CustomerID);
            if (customer != null)
            {
                this.lblCustomerGUID.Text = customer.CustomerGUID.ToString();
                this.lblEmail.Text = Server.HtmlEncode(customer.Email);
                this.lblFullName.Text = Server.HtmlEncode(customer.FullName);
                CommonHelper.SelectListItem(this.ddlAffiliate, customer.AffiliateID);
                this.cbIsAdmin.Checked = customer.IsAdmin;
                this.cbActive.Checked = customer.Active;
                this.lblRegistrationDate.Text = customer.RegistrationDate.ToString();

              

                //TODO add customer real-time shopping cart
            }
            else
                Response.Redirect("customers.aspx");
        }
       
        private void FillDropDowns()
        {
            this.ddlAffiliate.Items.Clear();
            ListItem ddlAffiliateItem = new ListItem("None (ID=0)", "0");
            this.ddlAffiliate.Items.Add(ddlAffiliateItem);
            AffiliateCollection affiliateCollection = AffiliateManager.GetAllAffiliates();
            foreach (Affiliate affiliate in affiliateCollection)
            {
                ListItem ddlAffiliateItem2 = new ListItem(affiliate.LastName + " (ID=" + affiliate.AffiliateID.ToString() + ")", affiliate.AffiliateID.ToString());
                this.ddlAffiliate.Items.Add(ddlAffiliateItem2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Customer customer = CustomerManager.GetByCustomerID(this.CustomerID);

                    if (customer != null)
                    {
                        customer = CustomerManager.UpdateCustomer(customer.CustomerID, customer.CustomerGUID, customer.Email, customer.PasswordHash,
                            customer.SaltKey, int.Parse(this.ddlAffiliate.SelectedItem.Value),
                           customer.LanguageID,
                            customer.CurrencyID, cbIsAdmin.Checked, cbActive.Checked, customer.Deleted, customer.RegistrationDate);

                    }

                    Response.Redirect("CustomerDetails.aspx?CustomerID=" + customer.CustomerID.ToString());
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
        
        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerManager.MarkCustomerAsDeleted(this.CustomerID);

                Response.Redirect("customers.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void AddBillingAddress_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("AddressAdd.aspx?CustomerID={0}&IsBillingAddress={1}", CustomerID, true));
        }

        protected void AddShippingAddress_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("AddressAdd.aspx?CustomerID={0}&IsBillingAddress={1}", CustomerID, false));
        }
        
        public int CustomerID
        {
            get
            {
               return CommonHelper.QueryStringInt("CustomerID");
            }
        }
    }
}
